angular.module('Controller')
.controller 'WFHeaderCtrl', ($scope,userData,$http,commonConstant, $rootScope,$localStorage,urls)->
    $scope.$storage = $localStorage
    $rootScope.$on('refresh-msg',()->
        loadMessageInfo()
    )
    $scope.preFix = urls.sys_backend

    _.extend $scope,
        user:userData.getUser()
        themeBg:
            background:commonConstant.topBg
        themeFontColor:
            color:commonConstant.topFontColor
        msgInfo:{}
        dropUser:->
            if  $('#userLabel').hasClass('open')
                $('#userLabel').removeClass('open')
            else
                $('#userLabel').addClass('open')
            ''
        goMyMsg:->
            return
        changePwd:->
            $('#userLabel').removeClass('open')
            data = [{
                id:0,
                order:0,
                type:0,
                title:'修改密码',
                url:"#{$scope.preFix}/Sa/Member/changePassword.html?r=0.30375724422863226",
                hideTitle:false,
                containerStyle:{},
                width:'100%',
                height:'700px'
            }]
            $rootScope.$broadcast('refreshLayoutContainer',data)
        loginOut:->
            userData.login()
        addUrlModule:(type)->
            $scope.saveUrlModule(true)
            $localStorage.setting = true
            console.log $localStorage.currentMenu
            if !$localStorage.currentMenu.urlModules.length
                length = 0
            else
                length = $localStorage.currentMenu.urlModules.length
            tmp = {
                id:length
                order:length
                type:type
                title:"模块名称#{length}"
                url:'http://www.sunagy.com:12980/Sa/Index/index'
                hideModule:1
                shortcuts:[]
                width:'80%'
                height:'300px'
            }
            _.each $localStorage.menuList,(menu)->
                if menu.id == $localStorage.currentMenu.id
                    menu.urlModules.push tmp
#            $rootScope.$broadcast('refreshLayoutContainer')
        saveUrlModule:(refresh)->
            $localStorage.setting = false
            tmp = []
            _.each $(".item"),(item)->
                w= $(angular.element(item).find("input")[0]).val()
                h= $(angular.element(item).find("input")[1]).val()
                title = $(angular.element(item).find("input")[2]).val()
                url = $(angular.element(item).find("input")[3]).val()
                order = $(angular.element(item).find("input")[4]).val()
                translates = $(item).css("transform");
                translateX = parseFloat(translates.substring(7).split(',')[4]);
                translateY = parseFloat(translates.substring(7).split(',')[5]);
                tmp.push {order:order,x:translateX,y:translateY,w:"#{w}%",h:"#{h}px",title:title,url:url}
            yyy = _.groupBy tmp,(item)->
                item.y
            zzz = []
            _.each yyy,(group)->
                xxx = _.sortBy group,(item)->
                    item.x
                zzz = zzz.concat(xxx)
            console.log zzz
            tmpModules = []
            _.each zzz,(item,index)->
                tmpModules.push $localStorage.currentMenu.urlModules[parseInt(item.order)]
                tmpModules[index].order = index
                tmpModules[index].width = item.w
                tmpModules[index].height = item.h
                tmpModules[index].title = item.title
                tmpModules[index].url = item.url
            $localStorage.currentMenu.urlModules = tmpModules
            _.each $localStorage.menuList,(menu)->
                if menu.id == $localStorage.currentMenu.id
                    menu = $localStorage.currentMenu
                    console.log menu
#            if refresh
#                $rootScope.$broadcast('refreshLayoutContainer')
        setting:->
            $localStorage.setting = true
#            $rootScope.$broadcast('refreshLayoutContainer')










